<?php
/**
 * $Horde: groo/collections.php,v 1.31.2.5 2006/01/01 21:29:05 jan Exp $
 *
 * Copyright 2002-2006 Joel Vandal <jvandal@infoteck.qc.ca>
 * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz>
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Joel Vandal <jvandal@infoteck.qc.ca>
 * @author  Mike Cochrane <mike@graftonhall.co.nz>
 * @since   Groo 2.0
 * @package Groo
 */

@define('GROO_BASE', dirname(__FILE__));
require_once GROO_BASE . '/lib/base.php';

/* Exit if this isn't an authenticated user. */
if (!Auth::getAuth()) {
    require GROO_BASE . '/list.php';
    exit;
}

/* Run through the action handlers. */
$actionID = Util::getFormData('actionID');
switch ($actionID) {
case 'save':
    $to_edit = Util::getFormData('edit_share');
    $id = Util::getFormData('id');
    if (empty($id)) {
        $notification->push(_("Collections must have a name."), 'horde.error');
        break;
    }

    $collection = '';
    if (!strlen($to_edit)) {
        /* Create New Share. */
        $collection = $GLOBALS['groo_shares']->newShare(md5(microtime()));
        $collection->set('name', $id);
        $collection->set('desc', Util::getFormData('description', ''));
        $result = $GLOBALS['groo_shares']->addShare($collection);

        if (is_a($result, 'PEAR_Error')) {
            $notification->push(sprintf(_("The collection \"%s\" couldn't be created: %s"), $id, $result->getMessage()), 'horde.error');
        } else {
            $notification->push(sprintf(_("The collection \"%s\" has been created."), $id), 'horde.success');
        }
    } else {
        $collection = &$GLOBALS['groo_shares']->getShare($to_edit);
        $collection->set('name', $id);
        $collection->set('desc', Util::getFormData('description', ''));
        $collection->save();

        $notification->push(sprintf(_("The collection \"%s\" has been saved."), $id), 'horde.success');
    }

    unset($to_edit);
    break;

case 'delete':
    $to_delete = Util::getFormData('edit_share');
    if (!$to_delete) {
        $notification->push(_("You must select a collection to be deleted."), 'horde.warning');
        break;
    }

    $share = $GLOBALS['groo_shares']->getShare($to_delete);
    if (is_a($share, 'PEAR_Error')) {
        $notification->push($share, 'horde.error');
        break;
    }

    /* Delete the collection. */
    require_once GROO_BASE . '/lib/Driver.php';
    $storage = &Groo_Driver::singleton($to_delete);
    $result = $storage->deleteAll();
    if (is_a($result, 'PEAR_Error')) {
        $notification->push(sprintf(_("Unable to delete \"%s\": %s"), $share->get('name'), $result->getMessage()), 'horde.error');
        break;
    } else {
        /* Remove share and all groups/permissions. */
        $GLOBALS['groo_shares']->removeShare($share);
        $notification->push(sprintf(_("The collection \"%s\" has been deleted."), $share->get('name')), 'horde.success');
    }

    /* Make sure we still own at least one collection. */
    if (!count(Groo::listCollections(true))) {
        /* If this share doesn't exist then create it. */
        if (!$GLOBALS['groo_shares']->exists(Auth::getAuth())) {
            require_once 'Horde/Identity.php';
            $identity = &Identity::singleton();
            $name = $identity->getValue('fullname');
            if (trim($name) == '') {
                $name = Auth::removeHook(Auth::getAuth());
            }
            $share = $GLOBALS['groo_shares']->newShare(Auth::getAuth());
            $share->set('name', sprintf(_("%s's Collection"), $name));
            $GLOBALS['groo_shares']->addShare($share);
        }
    }
    break;
}

/* Personal Item Lists */
$personal_collections = Groo::listCollections(true);

Horde::addScriptFile('popup.js', 'horde', true);
$title = _("Item Lists");
require GROO_TEMPLATES . '/common-header.inc';
require GROO_TEMPLATES . '/menu.inc';
require GROO_TEMPLATES . '/collections/collections.inc';
require $registry->get('templates', 'horde') . '/common-footer.inc';
